# -*- coding: utf-8 -*-

from flask import g

from google.appengine.ext import db
from google.appengine.api.namespace_manager import set_namespace

from dicthash.for_testing import TestWithApp
from dicthash.decorators import login_required_decor_factory
from dicthash.utils import Bunch

from lenta.code import register

login_required = login_required_decor_factory(lambda: g.user, lambda: '/login')


class TestFunctionalBase(TestWithApp):
    """ """
    def setUp(self):
        super(TestFunctionalBase, self).setUp()
        
        set_namespace('')
        self.user = Bunch(key=db.Key.from_path('User', "testusername"))
        self.namespace = self.user.key.name()
        
        def check_user():
            g.user = self.user
            if self.user:
                set_namespace(self.namespace)
        self.app.before_request(check_user)
        
        register(self.app, '/lenta',
            dict(login_required=login_required)
        )

    def tearDown(self):
        set_namespace('')
        super(TestFunctionalBase, self).tearDown()
                